package ogmios.processes;

import javax.swing.*;
import java.io.*;
import java.lang.reflect.*;

import ogmios.core.*;

/**
 * A thread that will wait for and execute code at the completion of a process
 */
public class DefaultProcessWaiter extends Thread
{
    /**
     * Returns true if the ProcessWaiter is waiting. It should only be read, not modified.<br>
     * This field may be deprecated in the future/
     */
    public volatile boolean isExecuting = false;
    public Process myProcess;
    public Runnable finalizer;
    /**
    * Creates process waiter
    * @param     newProcess     The process to wait on
    * @param     newFinalizer     The chunk of code to run after the process is complete
    */
    public DefaultProcessWaiter(Process newProcess, Runnable newFinalizer)
    {
        myProcess = newProcess;
        finalizer = newFinalizer;
    }
    public void run()
    {
        try
        {
            isExecuting = true;
            int c = myProcess.waitFor();
            if (finalizer != null)
            {
                SwingUtilities.invokeAndWait(finalizer);
            }

        }
        catch (InterruptedException ex)
        {
            OgmiosUtilities.showErrorBox(null, ex, "Process:Waiting Error");
        }
        catch (InvocationTargetException e)
        {
            OgmiosUtilities.showErrorBox(null, e, "Process:Waiting Error");
        }

    }


}

